package com.ithinks.flow.common.utils.middleware.rabbitMQ.workQueue;

import com.ithinks.flow.common.utils.middleware.rabbitMQ.ConnectionUtils;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import net.sf.json.JSONObject;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

/**
 * @Description 多个消费者消费同一队列的数据
 * @ClassName WorkQueue
 * @Author MR.LI
 * @date 2020.11.25 19:32
 */
public class WorkQueueProducer {

    private final static String QUEUE_NAME = "register";

    private static String IP = "localhost";
    //端口
    private static int PORT = 5672;
    //虚拟机IP，默认/
    private static String VHOST = "/";
    //队列名称
    private static String USERNAME = "guest";

    private static String PASSWORD = "guest";

    public static void main(String[] args) throws TimeoutException {

        Connection connection = ConnectionUtils.getConnection(IP,PORT,VHOST,USERNAME,PASSWORD);

        try {
            Channel channel = connection.createChannel();
            channel.queueDeclare(QUEUE_NAME,false,false,false,null);;
            JSONObject jsonObjet1=new JSONObject();
            jsonObjet1.put("msgType", "email");//该消息是针对发送验证邮件的。
            jsonObjet1.put("content", "执行发送验证邮件到邮箱操作");
            String message1=jsonObjet1.toString();
            channel.basicPublish("", QUEUE_NAME, null, message1.getBytes());//发布第一个异步消息
            System.out.println(channel+" Sent '"+message1+"'");
            JSONObject jsonObject2=new JSONObject();
            jsonObject2.put("msgType", "log");//该消息针对存储操作日志
            jsonObject2.put("content", "执行存储操作日志的操作");
            String message2=jsonObject2.toString();
            channel.basicPublish("", QUEUE_NAME, null, message2.getBytes());//发布第二个异步消息
            System.out.println(channel+" Sent '"+message2+"'");
            channel.close();
            connection.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}
